<?php

/**
 * Description of az_News
 *
 * @author Shark
 */
class Az_News {

    //Properties of new 
    var $news_id;
    var $title;
    var $author;
    var $description;
    var $tags;
    var $content;
    var $image;
    var $status;
    var $slug;
    var $date_create;
    var $date_edit;
    var $user_id;
    var $comment_stt;
    var $view;
    var $hot;
    var $survey_id;
    var $category_id;
    //Table name
    var $nametbl = 'az_news';

    //Function construct of new
    public function __construct() {
        
    }

    //Create object new
    function createNew($_newsId, $_title, $_author, $_description, $_content, $_image, $_status, $_slug, $_date_create, $_date_edit, $_userId, $_comment_stt, $_view, $_hot, $_surveyId, $_categoryId, $_tags) {
        $this->news_id = $_newsId;
        $this->title = $_title;
        $this->author = $_author;
        $this->description = $_description;
        $this->content = $_content;
        $this->image = $_image;
        $this->status = $_status;
        $this->slug = $_slug;
        $this->date_create = $_date_create;
        $this->date_edit = $_date_edit;
        $this->user_id = $_userId;
        $this->comment_stt = $_comment_stt;
        $this->view = $_view;
        $this->hot = $_hot;
        $this->survey_id = $_surveyId;
        $this->category_id = $_categoryId;
        $this->tags = $_tags;
    }

    /*
     * Function using for add new to database
     * @return true or false
     * 
     */

    function addNews() {
        global $az_db;
        if ($this->title != '' && $this->content != '') {
            $sql = "INSERT INTO " . $this->nametbl .
                    "(`title`,`author`,`description`,`tags`,`content`,`image`,`status`,`slug`," .
                    "`date_create`,`date_edit`,`user_id`,`comment_status`,`category_id`,`hot`,`survey_id`" .
                    ") VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            $params = array($this->title, $this->author, $this->description, $this->tags, $this->content, $this->image,
                $this->status, $this->slug, date('d/m/Y'), date('d/m/Y'), $this->user_id, $this->status, $this->category_id
                , $this->hot, $this->survey_id);
            if ($az_db->executeStmt_Az($sql, 'ssssssisssiisii', $params)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /*
     * Function using for update new  
     */

    function updateNews() {
        global $az_db;
        if ($this->title != null) {
            $type_format = '';
            $params = array();
            $sql = "UPDATE " . $this->nametbl . " SET title=?";
            $type_format .='s';
            array_push($params, $this->title);

            if ($this->author != '') {
                $sql .=',author=?';
                $type_format .='s';
                array_push($params, $this->author);
            }
            if ($this->description != '') {
                $sql.=',description=?';
                $type_format .='s';
                array_push($params, $this->description);
            }
            if ($this->tags != '') {
                $sql.=',tags=?';
                $type_format .='s';
                array_push($params, $this->tags);
            }
            if ($this->content != '') {
                $sql.=',content=?';
                $type_format .='s';
                array_push($params, $this->content);
            }
            if ($this->image != '') {
                $sql.=',image=?';
                $type_format .='s';
                array_push($params, $this->image);
            }
            if ($this->status != '') {
                $sql .=',status=?';
                $type_format .='i';
                array_push($params, $this->status);
            }
            if ($this->slug != '') {
                $sql .=',slug=?';
                $type_format .='s';
                array_push($params, $this->slug);
            }
            if ($this->date_edit != '') {
                $sql.=',date_edit=?';
                $type_format .='s';
                array_push($params, $this->date_edit);
            }
            if ($this->user_id != '') {
                $sql.=',user_id =?';
                $type_format.='i';
                array_push($params, $this->user_id);
            }
            if ($this->comment_stt != '') {
                $sql.=',comment_status=?';
                $type_format.='i';
                array_push($params, $this->comment_stt);
            }
            if ($this->category_id != '') {
                $sql.=',category_id=?';
                $type_format.='s';
                array_push($params, $this->category_id);
            }
            if ($this->hot != '') {
                $sql.=',hot=?';
                $type_format .='i';
                array_push($params, $this->hot);
            }
            if ($this->survey_id != '') {
                $sql.=',survey_id=?';
                $type_format .='i';
                array_push($params, $this->survey_id);
            }
            //where clause
            $sql.=" WHERE news_id=?";
            $type_format .='i';
            array_push($params, $this->news_id);
            return $az_db->executeStmt_Az($sql, $type_format, $params);
        }
    }

    /*
     * Function delete a new with a category id
     */

    function deleteNew($newId) {
        global $az_db;   
        $sql = "DELETE FROM " . $this->nametbl . " WHERE news_id=" . $newId;
        return $az_db->executeSQL_Query($sql);
    }

}

?>
